Click here to return to the VHDL Reference Guide. (last edit: 24. september 2012)

Constant declaration

A constant is used to give a name to a value, in order to make code easier to read and maintain.

Syntax

    constant-declaration ->
      constant identifier { ',' identifier } ':'' subtype-indication [ ':=' Expression]';'
    
---------------------------------------------------------------------------------------------------
    subtype-indication ->
      [resolution-function-name] type-name [ range-contraint | index-constraint ]
    

Example

    constant Width: POSITIVE := 8;
    constant Mask: STD_LOGIC_VECTOR := "0001000011111";  -- Range determined by length of string
    

Rules and Comments

The value of a constant is defined at declaration and cannot be changed using an assignment statement.
The value of a constant can only be read.
A constant can be declared using the so called deferred constant declaration. It is then declared and named in the package declaration, but gets its value first in the package body. This coding style hides the value of the constant, it is not shown in the package declaration, and the user is not tempted to use the value directly. Another advantage is if the value of the constant is modified. Then only the package body needs to be recompiled.

Placement

      package Pack is
        ...  <- HERE
      end package Pack;
      package body Pack is
        ...  <- HERE
      end package body Pack;
      Blk:block
        ...  <- HERE
      begin
        ...
      end block Blk;
      entity Ent is
        ...  <- HERE
      begin
        ...
      end entity Ent;
      architecture Arc of Ent is
        ...  <- HERE
      begin
        ...
      end architecture Arc;
      configuration Conf of Ent is
        ...
      end configuration Conf;
      Proc:process(...)
        ...  <- HERE
      begin
        ...
      end process Proc;
      procedure P(...) is
        ...  <- HERE
      begin
        ...
      end procedure P;
      function F(...) return Tp is
        ...  <- HERE
      begin
        ...
      end function F;

See Also

Alias, Variable